#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"

/* widgets:
Layer_Cdaos_SetMode
Background_Cdaos_SetMode
PopupButton_ModeSetBack
Container_Cdaos_SetMode6
PopupButton_Cdaos_SetMode6_On
PopupButton_Cdaos_SetMode6_Off
Container_Cdaos_SetMode5
PopupButton_Cdaos_SetMode5_On
PopupButton_Cdaos_SetMode5_Off
Container_Cdaos_SetMode4
PopupButton_Cdaos_SetMode4_On
PopupButton_Cdaos_SetMode4_Off
Container_Cdaos_SetMode3
PopupButton_Cdaos_SetMode3_On
PopupButton_Cdaos_SetMode3_Off
Container_Cdaos_SetMode2
PopupButton_Cdaos_SetMode2_On
PopupButton_Cdaos_SetMode2_Off
Container_Cdaos_SetMode1
PopupButton_Cdaos_SetMode1_On
PopupButton_Cdaos_SetMode1_Off
Container_Cdaos_SetMode0
PopupButton_Cdaos_SetMode0_On
PopupButton_Cdaos_SetMode0_Off
*/
//模式选项更具配置显示
void Mode_Item_Display_By_Config(void);
//当前模式显示
void Cdaos_Mode_Update(uint8_t mode);
//刷新当前模式文字
void Cdaos_Mode_Text_Update(uint8_t mode);

static ITUBackground* Background_Cdaos_SetMode;
static ITUContainer* Container_Cdaos_SetMode[7];
static ITUPopupButton* PopupButton_Cdaos_SetMode_On[7];
static ITUText* Text_Cdaos_SetMode;
bool CdaosSetModeOnEnter(ITUWidget* widget, char* param)
{
	uint8_t i; 
	char TempName[64];
	if (!Background_Cdaos_SetMode)
	{
		Background_Cdaos_SetMode = ituSceneFindWidget(&theScene, "Background_Cdaos_SetMode");
		assert(Background_Cdaos_SetMode);
		Text_Cdaos_SetMode = ituSceneFindWidget(&theScene, "Text_Cdaos_SetMode");
		assert(Text_Cdaos_SetMode);
		for (i = 0; i < 7; i++)
		{
			sprintf(TempName, "Container_Cdaos_SetMode%d", i);
			Container_Cdaos_SetMode[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Container_Cdaos_SetMode[i]);
			sprintf(TempName, "PopupButton_Cdaos_SetMode%d_On", i);
			PopupButton_Cdaos_SetMode_On[i] = ituSceneFindWidget(&theScene, TempName);
			assert(PopupButton_Cdaos_SetMode_On[i]);
		}
	}
	//模式选项更具配置显示
	Mode_Item_Display_By_Config();
	//当前模式显示
	Cdaos_Mode_Update(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Cdaos_Status.Mode);
	Cdaos_Mode_Text_Update(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Cdaos_Status.Mode);
    return true;
}

bool CdaosSetModeOnTimer(ITUWidget* widget, char* param)
{
	MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
	Cdaos_Mode_Update(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Cdaos_Status.Mode);
    return true;
}

bool CdaosSetModeOnLeave(ITUWidget* widget, char* param)
{
	MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
    return true;
}

bool Cdaos_SetMode(ITUWidget* widget, char* param)
{
	uint8_t Mode_Temp;
	Mode_Temp = atoi(param);
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Cdaos_Status.Mode = Mode_Temp;
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Cdaos_Status.status = CMD_CDOAS_SETMODE;//设置模式
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Cdaos_Status.Mode = Mode_Temp;
	printf("Moorgen INFO : Touch to set the Cdoas's mode.\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

//模式选项更具配置显示
void Mode_Item_Display_By_Config(void)
{
	uint8_t i;
	for (i = 0; i < 7; i++)
	{
		if ((TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_Config_Value.IT_Cdaos_Config.Mode_Suppot >> i) & 0x01)
			ituWidgetSetVisible(Container_Cdaos_SetMode[i], true);
		else
			ituWidgetSetVisible(Container_Cdaos_SetMode[i], false);
	}
}
void Cdaos_Mode_Text_Update(uint8_t mode)
{
#ifndef _WIN32
	//Text
	if (Sys.Language == MG_LANGUAGE_CHS)
	{
		if (mode == 0)
			ituTextSetString(Text_Cdaos_SetMode, "当前无模式");
		else if (mode == 1)
			ituTextSetString(Text_Cdaos_SetMode, "自动换气");
		else if (mode == 2)
			ituTextSetString(Text_Cdaos_SetMode, "全热换气");
		else if (mode == 3)
			ituTextSetString(Text_Cdaos_SetMode, "普通换气");
		else if (mode == 4)
			ituTextSetString(Text_Cdaos_SetMode, "新风");
		else if (mode == 5)
			ituTextSetString(Text_Cdaos_SetMode, "除湿");
		else if (mode == 6)
			ituTextSetString(Text_Cdaos_SetMode, "内循环");
		else if (mode == 7)
			ituTextSetString(Text_Cdaos_SetMode, "其他模式");
	}
	else if (Sys.Language == MG_LANGUAGE_ENG)
	{
		if (mode == 0)
			ituTextSetString(Text_Cdaos_SetMode, "NULL");
		else if (mode == 1)
			ituTextSetString(Text_Cdaos_SetMode, "Auto");
		else if (mode == 2)
			ituTextSetString(Text_Cdaos_SetMode, "Heat change");
		else if (mode == 3)
			ituTextSetString(Text_Cdaos_SetMode, "Exchange");
		else if (mode == 4)
			ituTextSetString(Text_Cdaos_SetMode, "Outside loop");
		else if (mode == 5)
			ituTextSetString(Text_Cdaos_SetMode, "Arefaction");
		else if (mode == 6)
			ituTextSetString(Text_Cdaos_SetMode, "Circulation");
		else if (mode == 7)
			ituTextSetString(Text_Cdaos_SetMode, "Other");
	}
#else
	//Text
	if (Sys.Language == MG_LANGUAGE_CHS)
	{
		if (mode == 0)
			ituTextSetString(Text_Cdaos_SetMode, "NULL");
		else if (mode == 1)
			ituTextSetString(Text_Cdaos_SetMode, "自动换气");
		else if (mode == 2)
			ituTextSetString(Text_Cdaos_SetMode, "全热换气");
		else if (mode == 3)
			ituTextSetString(Text_Cdaos_SetMode, "换气");
		else if (mode == 4)
			ituTextSetString(Text_Cdaos_SetMode, "新风");
		else if (mode == 5)
			ituTextSetString(Text_Cdaos_SetMode, "除湿");
		else if (mode == 6)
			ituTextSetString(Text_Cdaos_SetMode, "循环");
		else if (mode == 7)
			ituTextSetString(Text_Cdaos_SetMode, "其他");
	}
	else if (Sys.Language == MG_LANGUAGE_ENG)
	{
		if (mode == 0)
			ituTextSetString(Text_Cdaos_SetMode, "NULL");
		else if (mode == 1)
			ituTextSetString(Text_Cdaos_SetMode, "Auto");
		else if (mode == 2)
			ituTextSetString(Text_Cdaos_SetMode, "Heat change");
		else if (mode == 3)
			ituTextSetString(Text_Cdaos_SetMode, "Exchange");
		else if (mode == 4)
			ituTextSetString(Text_Cdaos_SetMode, "Outside loop");
		else if (mode == 5)
			ituTextSetString(Text_Cdaos_SetMode, "Arefaction");
		else if (mode == 6)
			ituTextSetString(Text_Cdaos_SetMode, "Circulation");
		else if (mode == 7)
			ituTextSetString(Text_Cdaos_SetMode, "Other");
	}
#endif
}
//当前模式显示
void Cdaos_Mode_Update(uint8_t mode)
{
	static uint8_t mode_last = 0xFF;
	uint8_t i;
	if (mode_last == mode)return;
	mode_last = mode;
	for (i = 0; i < 7; i++)
	{
		ituWidgetSetVisible(PopupButton_Cdaos_SetMode_On[i], false);
	}
	if (mode != 0)
	{
		if (mode < 7)
			ituWidgetSetVisible(PopupButton_Cdaos_SetMode_On[mode-1], true);
		else
			ituWidgetSetVisible(PopupButton_Cdaos_SetMode_On[6], true);
	}
	Cdaos_Mode_Text_Update(mode);
}

